package com.sywl.glzh.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sywl.glzh.domain.Product;
import com.sywl.glzh.domain.ProductType;

@Controller
public class ProController {
	
	@RequestMapping({"pro", "en/pro"})
	public void pro(Model model) {
		model.addAttribute("products", Product.findAllProducts());
	}
	
	/**
	 * 产品中心
	 * @param 类型
	 * @param model
	 * @return
	 */
	@RequestMapping("pro/{type}")
	public String pro(@PathVariable long type, Model model) {
		ProductType pt = ProductType.findProductType(type);
		if(pt == null) {
			return "redirect:/404";
		}
		model.addAttribute("productType", pt);
		model.addAttribute("products", Product.findProductsByType(pt).getResultList());
		return "pro";
	}
	
	/**
	 * 产品中心 英文
	 * @param 类型
	 * @param model
	 * @return
	 */
	@RequestMapping("en/pro/{type}")
	public String enPro(@PathVariable long type, Model model) {
		String view = pro(type, model);
		if (view.contains("redirect")) {
			return view;
		}
		return "en/" + view;
	}
	
	/**
	 * 产品详细
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("pro/view/{id}")
	public String product(@PathVariable long id, Model model) {
		Product product = Product.findProduct(id);
		if(product == null) {
			return "redirect:404";
		}
		model.addAttribute("product", product);
		return "product";
	}
	
	/**
	 * 产品详细 英文
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("en/pro/view/{id}")
	public String enProduct(@PathVariable long id, Model model) {
		String view = product(id, model);
		if (view.contains("redirect")) {
			return view;
		}
		return "en/" + view;
	}
	
	/**
	 * 中文搜索
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping("search")
	public String search(String content, Model model) {
		model.addAttribute("products", Product.findProductsByNameLike("%" + content.trim() + "%").getResultList());
		return "pro";
	}
	
	/**
	 * 英文搜索
	 * @param content
	 * @param model
	 * @return
	 */
	@RequestMapping("en/search")
	public String enSearch(String content, Model model) {
		model.addAttribute("products", Product.findProductsByEnNameLike("%" + content.trim() + "%").getResultList());
		return "en/pro";
	}
	
}
